/***************************************************************************************
 *
 *  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
 *
 *  By downloading, copying, installing or using the software you agree to this license.
 *  If you do not agree to this license, do not download, install, 
 *  copy or use the software.
 *
 *  Copyright (C) 2010-2014, Happytimesoft Corporation, all rights reserved.
 *
 *  Redistribution and use in binary forms, with or without modification, are permitted.
 *
 *  Unless required by applicable law or agreed to in writing, software distributed 
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 *  language governing permissions and limitations under the License.
 *
****************************************************************************************/

#include "sys_inc.h"
#include "rc4.h"


void rc4_setup(struct rc4_state *s, unsigned char *key,  int length)
{
	int		i;
	int		j;
	int		k;
	int		*m;
	int		a;

	s->x=0;
	s->y=0;
	m=s->m;

	m[0]=0;
	m[1]=1;
	m[2]=2;
	m[3]=3;
	m[4]=4;
	m[5]=5;
	m[6]=6;
	m[7]=7;
	m[8]=8;
	m[9]=9;
	m[10]=10;
	m[11]=11;
	m[12]=12;
	m[13]=13;
	m[14]=14;
	m[15]=15;
	m[16]=16;
	m[17]=17;
	m[18]=18;
	m[19]=19;
	m[20]=20;
	m[21]=21;
	m[22]=22;
	m[23]=23;
	m[24]=24;
	m[25]=25;
	m[26]=26;
	m[27]=27;
	m[28]=28;
	m[29]=29;
	m[30]=30;
	m[31]=31;
	m[32]=32;
	m[33]=33;
	m[34]=34;
	m[35]=35;
	m[36]=36;
	m[37]=37;
	m[38]=38;
	m[39]=39;
	m[40]=40;
	m[41]=41;
	m[42]=42;
	m[43]=43;
	m[44]=44;
	m[45]=45;
	m[46]=46;
	m[47]=47;
	m[48]=48;
	m[49]=49;
	m[50]=50;
	m[51]=51;
	m[52]=52;
	m[53]=53;
	m[54]=54;
	m[55]=55;
	m[56]=56;
	m[57]=57;
	m[58]=58;
	m[59]=59;
	m[60]=60;
	m[61]=61;
	m[62]=62;
	m[63]=63;
	m[64]=64;
	m[65]=65;
	m[66]=66;
	m[67]=67;
	m[68]=68;
	m[69]=69;
	m[70]=70;
	m[71]=71;
	m[72]=72;
	m[73]=73;
	m[74]=74;
	m[75]=75;
	m[76]=76;
	m[77]=77;
	m[78]=78;
	m[79]=79;
	m[80]=80;
	m[81]=81;
	m[82]=82;
	m[83]=83;
	m[84]=84;
	m[85]=85;
	m[86]=86;
	m[87]=87;
	m[88]=88;
	m[89]=89;
	m[90]=90;
	m[91]=91;
	m[92]=92;
	m[93]=93;
	m[94]=94;
	m[95]=95;
	m[96]=96;
	m[97]=97;
	m[98]=98;
	m[99]=99;
	m[100]=100;
	m[101]=101;
	m[102]=102;
	m[103]=103;
	m[104]=104;
	m[105]=105;
	m[106]=106;
	m[107]=107;
	m[108]=108;
	m[109]=109;
	m[110]=110;
	m[111]=111;
	m[112]=112;
	m[113]=113;
	m[114]=114;
	m[115]=115;
	m[116]=116;
	m[117]=117;
	m[118]=118;
	m[119]=119;
	m[120]=120;
	m[121]=121;
	m[122]=122;
	m[123]=123;
	m[124]=124;
	m[125]=125;
	m[126]=126;
	m[127]=127;
	m[128]=128;
	m[129]=129;
	m[130]=130;
	m[131]=131;
	m[132]=132;
	m[133]=133;
	m[134]=134;
	m[135]=135;
	m[136]=136;
	m[137]=137;
	m[138]=138;
	m[139]=139;
	m[140]=140;
	m[141]=141;
	m[142]=142;
	m[143]=143;
	m[144]=144;
	m[145]=145;
	m[146]=146;
	m[147]=147;
	m[148]=148;
	m[149]=149;
	m[150]=150;
	m[151]=151;
	m[152]=152;
	m[153]=153;
	m[154]=154;
	m[155]=155;
	m[156]=156;
	m[157]=157;
	m[158]=158;
	m[159]=159;
	m[160]=160;
	m[161]=161;
	m[162]=162;
	m[163]=163;
	m[164]=164;
	m[165]=165;
	m[166]=166;
	m[167]=167;
	m[168]=168;
	m[169]=169;
	m[170]=170;
	m[171]=171;
	m[172]=172;
	m[173]=173;
	m[174]=174;
	m[175]=175;
	m[176]=176;
	m[177]=177;
	m[178]=178;
	m[179]=179;
	m[180]=180;
	m[181]=181;
	m[182]=182;
	m[183]=183;
	m[184]=184;
	m[185]=185;
	m[186]=186;
	m[187]=187;
	m[188]=188;
	m[189]=189;
	m[190]=190;
	m[191]=191;
	m[192]=192;
	m[193]=193;
	m[194]=194;
	m[195]=195;
	m[196]=196;
	m[197]=197;
	m[198]=198;
	m[199]=199;
	m[200]=200;
	m[201]=201;
	m[202]=202;
	m[203]=203;
	m[204]=204;
	m[205]=205;
	m[206]=206;
	m[207]=207;
	m[208]=208;
	m[209]=209;
	m[210]=210;
	m[211]=211;
	m[212]=212;
	m[213]=213;
	m[214]=214;
	m[215]=215;
	m[216]=216;
	m[217]=217;
	m[218]=218;
	m[219]=219;
	m[220]=220;
	m[221]=221;
	m[222]=222;
	m[223]=223;
	m[224]=224;
	m[225]=225;
	m[226]=226;
	m[227]=227;
	m[228]=228;
	m[229]=229;
	m[230]=230;
	m[231]=231;
	m[232]=232;
	m[233]=233;
	m[234]=234;
	m[235]=235;
	m[236]=236;
	m[237]=237;
	m[238]=238;
	m[239]=239;
	m[240]=240;
	m[241]=241;
	m[242]=242;
	m[243]=243;
	m[244]=244;
	m[245]=245;
	m[246]=246;
	m[247]=247;
	m[248]=248;
	m[249]=249;
	m[250]=250;
	m[251]=251;
	m[252]=252;
	m[253]=253;
	m[254]=254;
	m[255]=255;

	j=k=0;

	for (i=0; i<256; i++)
	{
		a=m[i];
		j=(unsigned char)(j+a+key[k]);
		m[i]=m[j];
		m[j]=a;
		if (++k >= length) k=0;
	}
}

void rc4_crypt(struct rc4_state *s, unsigned char *data, int length)
{
	int		i;
	int		x;
	int		y;
	int		*m;
	int		a;
	int		b;

	x=s->x;
	y=s->y;
	m=s->m;

	for (i=0; i<length; i++)
	{
		x=(unsigned char) (x+1);
		a=m[x];
		y=(unsigned char)(y+a);
		m[x]=b=m[y];
		m[y]=a;
		data[i]^=m[(unsigned char)(a+b)];
	}

	s->x=x;
	s->y=y;
}


